/*
 * @Author: zhengzhuang
 * @since: 2020-01-08 15:11:41
 * @lastTime     : 2020-01-09 10:51:55
 * @LastAuthor   : zhengzhuang
 * @文件相对于项目的路径: /wangyueche_driver/lib/pages/real_time_guest_pulling_module/itinerary_order/free_ride_order.dart
 * @message: 行程订单城际订单   FreeRideOrder
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:C20Driver/provider/driver_information.dart';
import 'package:C20Driver/provider/order_information.dart';
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:C20Driver/service/request.dart';
import 'package:C20Driver/utils/toast.dart';
import 'package:C20Driver/widget/no_data_widget.dart';
import 'package:C20Driver/widget/spinkit.dart';
import 'package:provider/provider.dart';
import '../../../model/get_trip_order_list.dart';

class FreeRideOrder extends StatefulWidget {
  @override
  _FreeRideOrderState createState() => _FreeRideOrderState();
}

class _FreeRideOrderState extends State<FreeRideOrder>
    with AutomaticKeepAliveClientMixin {
  ScrollController _scrollController = new ScrollController();

  @override
  bool get wantKeepAlive => true;

  List _list;
  int _page = 1;

  @override
  void initState() {
    super.initState();
    _getItineraryOrder();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        _getItineraryOrderPullUpLoading();
      }
    });
  }

  /// 获取行程订单列表 分为历史行程订单和行程中订单
  Future _getItineraryOrder() async {
    _list =
        await Request.getItineraryOrder(page: _page.toString(), type: '0040');
    if (mounted) {
      setState(() {
        _list = _list;
      });
    }
  }

  /// 上拉加载我的行程列表
  Future _getItineraryOrderPullUpLoading() async {
    List _value = await Request.getItineraryOrder(
        page: (_page + 1).toString(), type: '0040');
    if (mounted) {
      if (_value.length != 0) {
        setState(() {
          _page++;
          _list.addAll(_value);
        });
      } else {
        ToastX.center('没有更多数据');
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return _list == null
        ? Spinkit()
        : Container(
            alignment: Alignment.center,
            child: _list.length != 0
                ? ListView.builder(
                    controller: _scrollController,
                    itemCount: _list == null ? 0 : _list.length,
                    itemBuilder: (context, index) {
                      var item = (_list)[index];
                      return _itineraryOrderCard(item);
                    },
                  )
                : NoDataWidget(),
          );
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  // 行程订单卡片
  Widget _itineraryOrderCard(val) {
    var getTripOrderListData = GetTripOrderList.fromJson(val);
    return Container(
      width: ScreenUtil().setWidth(700.0),
      padding: EdgeInsets.only(
        left: ScreenUtil().setWidth(25.0),
        right: ScreenUtil().setWidth(25.0),
      ),
      child: InkWell(
        child: Card(
          child: Container(
            padding: EdgeInsets.all(ScreenUtil().setWidth(25.0)),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                // Container(
                //   alignment: Alignment.centerLeft,
                //   child: Offstage(
                //     offstage: getTripOrderListData.status != '0030',
                //     child: Text(
                //       '未发起收款',
                //       style: TextStyle(
                //         fontSize: ScreenUtil().setSp(30.0),
                //         color: Colors.red
                //       ),
                //     ),
                //   ),
                // ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Icon(
                          Icons.access_time,
                          size: ScreenUtil().setWidth(16.0),
                          color: Color.fromARGB(255, 205, 205, 205),
                        ),
                        SizedBox(
                          width: ScreenUtil().setWidth(10.0),
                        ),
                        Text(
                          getTripOrderListData.depTime != null
                              ? getTripOrderListData.depTime
                              : '',
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(30.0),
                            color: Color.fromARGB(255, 102, 102, 102),
                          ),
                        ),
                      ],
                    ),
                    InkWell(
                      child: Text(
                        '${getOrderStatus(getTripOrderListData.status)}',
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(32.0),
                          color: Color.fromARGB(255, 255, 153, 0),
                        ),
                      ),
                    )
                  ],
                ),
                SizedBox(
                  height: ScreenUtil().setHeight(15.0),
                ),
                Row(
                  children: <Widget>[
                    Container(
                      width: ScreenUtil().setWidth(15.0),
                      height: ScreenUtil().setHeight(15.0),
                      decoration: BoxDecoration(
                        color: Color.fromARGB(255, 36, 142, 247),
                        shape: BoxShape.circle,
                      ),
                    ),
                    SizedBox(
                      width: ScreenUtil().setWidth(10.0),
                    ),
                    Container(
                      width: ScreenUtil().setWidth(400.0),
                      child: Text(
                        getTripOrderListData.depAre,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(30.0),
                          color: Color.fromARGB(255, 102, 102, 102),
                        ),
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: ScreenUtil().setHeight(15.0),
                ),
                Row(
                  children: <Widget>[
                    Container(
                      width: ScreenUtil().setWidth(15.0),
                      height: ScreenUtil().setHeight(15.0),
                      decoration: BoxDecoration(
                        color: Color.fromARGB(255, 255, 153, 0),
                        shape: BoxShape.circle,
                      ),
                    ),
                    SizedBox(
                      width: ScreenUtil().setWidth(10.0),
                    ),
                    Container(
                      width: ScreenUtil().setWidth(400.0),
                      child: Text(
                        getTripOrderListData.destArea,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(30.0),
                          color: Color.fromARGB(255, 102, 102, 102),
                        ),
                      ),
                    ),
                    Expanded(child: SizedBox()), //自动扩展挤压
                    Text(
                      '￥${getTripOrderListData.price.toString() != null ? getTripOrderListData.price.toString() : "0.0"}',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(32.0),
                        color: Colors.black,
                        fontWeight: FontWeight.w700,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
        onTap: () {
          if (getOrderStatus(getTripOrderListData.status) == '未完成') {
            NavigatorUtil.jump(context,
                    '/intercityPickUpPassengers?orderId=${getTripOrderListData.intercityId}')
                .then((value) {});
          } else {
            ToastX.bottom('订单${getOrderStatus(getTripOrderListData.status)}');
          }

          // if (provider.driverInfo.workingStatus == '0020' || provider.driverInfo.workingStatus == '0040') {
          // switch (getTripOrderListData.status) {
          //   case '0040':
          //     // 去接乘客
          //
          //     break;
          //   case '0045':
          //     // 到达乘客约定地点
          //     NavigatorUtil.jump(context,
          //             '/intercityPickUpPassengers?orderId=${getTripOrderListData.id}')
          //         .then((value) {
          //     });
          //     break;
          //   case '0055':
          //     // 等待乘客上车
          //     NavigatorUtil.jump(context,
          //             '/intercityPickUpPassengers?orderId=${getTripOrderListData.id}')
          //         .then((value) {
          //     });
          //     break;
          //   case '0060':
          //     // 到达目的地
          //     NavigatorUtil.jump(context,
          //             '/intercityPickUpPassengers?orderId=${getTripOrderListData.id}')
          //         .then((value) {
          //     });
          //     break;
          //   default:
          // }
        },
      ),
    );
  }

  ///订单状态 0000 :订单完成,已支付 0010 :司机接单 0020 :接到乘客,行程中 0030:行程结束,报价成功,等待支付  0040:报价失败,不可支付 0050:发起收款 0060订单取消 0100已关闭
  String getOrderStatus(String statu) {
    switch (statu) {
      case "0000":
        return "已完成";
      case '0050':
        return '已完成';
      case "0100":
        return '已关闭';
      default:
        return '未完成';
    }
  }
}
